package com.xunk.equipment.controller;

import com.xunk.common.core.annotation.EnumCheck;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.equipment.annotation.OperationLog;
import com.xunk.equipment.service.IAirSwitchService;
import com.xunk.equipment.service.mandun.enums.MandunCmdEnum;
import com.xunk.equipment.service.mandun.service.IMandunService;
import com.xunk.equipment.service.mandun.vo.IntervalRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ProjectName: xunk
 * @Package: com.xunk.equipment.controller
 * @ClassName: AirSwitchController
 * @Author: luanzhiwei
 * @Description: 空气开关
 * @Date: 2021/3/31 17:19
 * @Version: 1.0
 */
@RestController
@RequestMapping(value = "/airswitch/")
@Validated
public class AirSwitchController extends BaseController {

    @Autowired
    private IAirSwitchService airSwitchService;
    @Autowired
    private IMandunService mandunService;

    /**
     * 空气开关，控制各个开关，例如卧室独立空开，空调独立空开
     *
     * @param deviceSn ，设备
     * @param control
     * @param addr
     * @return
     */
    @GetMapping(value = "/mandun/control")
    @OperationLog(operateDesc = "控制曼顿空气开关")
    public AjaxResult changeSwitch(@RequestParam(name = "deviceSn") String deviceSn, @RequestParam(name = "control") @EnumCheck(message = "错误的控制命令", enableClass = MandunCmdEnum.class) String control,
                                   @RequestParam(name = "addr") String addr) {
        return airSwitchService.changeSwitchStatus(deviceSn, control, addr);
    }

    /**
     * 重命名空开设备
     *
     * @param deviceSn
     * @param addr
     * @return
     */
    @GetMapping(value = "/mandun/renameSwitch")
    @OperationLog(operateDesc = "重命名曼顿空气开关")
    public AjaxResult renameSwitch(@RequestParam(name = "deviceSn") String deviceSn, @RequestParam(name = "addr") String addr, @RequestParam(name = "name") String name) {
        return airSwitchService.renameSwitch(deviceSn, addr, name);
    }

    /**
     * 获取曼顿设备区间电量
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/mandun/getIntervalElectricityConsumption")
    public AjaxResult getIntervalElectricityConsumption(@Validated @RequestBody IntervalRequest request) {
        return mandunService.getInterval(request);
    }

    /**
     * 获取曼顿设备信息
     *
     * @param deviceSn
     * @return
     */
    @GetMapping(value = "/mandun/getBox")
    public AjaxResult getBox(@RequestParam(name = "deviceSn") String deviceSn) {
        return mandunService.getBox(deviceSn);
    }

    /**
     * 曼顿消息订阅
     *
     * @param pushType
     * @return
     */
    @GetMapping(value = "/mandun/subscribe")
    public AjaxResult subscribe(@RequestParam(name = "pushType") byte pushType) {
        return mandunService.subscribePushService(pushType);
    }

    /**
     * 曼顿取消消息订阅
     *
     * @param pushType
     * @return
     */
    @GetMapping(value = "/mandun/unsubscribe")
    public AjaxResult unsubscribe(@RequestParam(name = "pushType") byte pushType) {
        return mandunService.unsubscribe(pushType);
    }

    /**
     * 查询已订阅的消息
     *
     * @param pushType
     * @return
     */
    @GetMapping(value = "/mandun/querySubscribe")
    public AjaxResult querySubscribe(@RequestParam(name = "pushType") byte pushType) {
        return mandunService.querySubscribe(pushType);
    }

}
